//
//  YZSportMapViewController.m
//  runrun
//
//  Created by yongzhen on 16/10/21.
//  Copyright © 2016年 yongzhen. All rights reserved.
//

#import "YZSportMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "YZSportTrackingLine.h"
#import "YZCircleAnimator.h"
@interface YZSportMapViewController ()<MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation YZSportMapViewController
{
    BOOL _hasStartLocal;
    YZCircleAnimator *_circleAnim;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
        self.modalPresentationStyle = UIModalPresentationCustom;
        _circleAnim = [YZCircleAnimator new];
        self.transitioningDelegate = _circleAnim;
        
    }
    return self;
}
-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setupMapView];
    
}




-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (!updatingLocation) {
        return;
    }

    if (_sportTracking.sportStatus != YZSportStatusContinue) {
        return;
    }
    // 拖拽之后，继续追踪地图模式
    [mapView setCenterCoordinate:userLocation.coordinate animated:YES];

    
    if (!_hasStartLocal && _sportTracking.sportStartLocation != nil) {
        
        _hasStartLocal = YES;
        // 大头针
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
        annotation.coordinate = userLocation.location.coordinate;
        [mapView addAnnotation:annotation];
        
    }
    
    [mapView addOverlay:[_sportTracking appendLocation:userLocation.location]];
    [self updateUIdisplay];
    
    
    
}
-(void)updateUIdisplay
{
    _distanceLabel.text = [NSString stringWithFormat:@"%.02f",_sportTracking.totalDistance];
    
//    NSInteger totalTime = (NSInteger)_sportTracking.totalTime;
//    return [];
    _timeLabel.text = _sportTracking.totalTimeStr;
    
}

- (IBAction)stopMapView:(UIButton *)sender {
    
     [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}

// 点击地图回调
-(void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
//    [self dismissViewControllerAnimated:YES completion:nil];
    
//    NSLog(@"%f  %f",coordinate.latitude,coordinate.longitude);
//    
//    CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
//    
//    
//    YZSportTrackingLine *trackintgLine = [[YZSportTrackingLine alloc] initWithStartLocation:_startLocation endLocation:endLocation];
//
//    
// 
//    [mapView addOverlay:trackintgLine.polyline];
////    lastCoordinate = coordinate;
//
    
    
}



-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay{
    if (![overlay isKindOfClass:[MAPolyline class]]) {
        return nil;
    }
    // 折线渲染器
    YZSportPloyline *polyline = (YZSportPloyline *)overlay;
    
    MAPolylineRenderer *render = [[MAPolylineRenderer alloc] initWithPolyline:polyline];
    render.lineWidth = 5;
//    render.strokeColor = [UIColor greenColor];
    render.strokeColor = polyline.coler;
    
//    MAOverlayRenderer *
    return  render;
    
}



-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if (![annotation isKindOfClass:[MAPointAnnotation class]]) {
        return nil;
    }
    
    static NSString *annotaionID = @"annotaionID";
    MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotaionID];
    
    if (annotationView == nil) {
        annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotaionID];
        
    }
    UIImage *image = _sportTracking.sportImage;
    
    annotationView.image = image;
    annotationView.centerOffset = CGPointMake(0, -image.size.height * 0.5);
    
    return annotationView;
    
    
}



-(void)setupMapView
{
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:mapView atIndex:0];
    
    mapView.showsScale = NO;
    // 关闭地图的旋转
    mapView.rotateCameraEnabled = NO;
    
    mapView.showsUserLocation = YES;
    
    mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    mapView.allowsBackgroundLocationUpdates = YES;
    mapView.pausesLocationUpdatesAutomatically = NO;
    mapView.delegate = self;
    _mapView = mapView;
    
}

@end
